package cz.cuni.mff.ufal.volk;

import java.util.Locale;

import org.springframework.beans.factory.BeanFactory;

public class ApplicationSettings {

	private static ApplicationSettings current;
	private final static Object currentLock = new Object();

	private final static String CURRENT_SETTINGS_BEAN = "applicationSettings";
	private static BeanFactory factory;

	/**
	 * Gets the current application settings.
	 */
	public static ApplicationSettings getCurrent() {
		if (current == null) {
			synchronized (currentLock) {
				if (current == null) {
					current = factory.getBean(CURRENT_SETTINGS_BEAN, ApplicationSettings.class);
				}
			}
		}
		return current;
	}

	/**
	 * Sets the factory used to retrieve some parameters. The method can be called only once.
	 * @param  factory the factory to set
	 * @throws IllegalStateException if the factory has already been set
	 * @throws NullPointerException  if the {@code factory} parameter is {@code null}
	 */
	public static void setFactory(BeanFactory factory) {
		if (ApplicationSettings.factory != null)
			throw new IllegalStateException("An attempt was made to set the bean factory more than once");
		factory.getClass();

		ApplicationSettings.factory = factory;
	}

	//[start] properties

	private Locale locale = Locale.US;

	/**
	 * Gets the default locale used to communicate with the user.
	 * @return
	 */
	public Locale getLocale() {
		return locale;
	}

	/**
	 * Sets the default locale used to communicate with the user.
	 * @param  value  locale to set
	 * @throws NullPointerException if {@code value} param is {@code null}
	 */
	public void setLocale(Locale value) {
		value.getClass();
		locale = value;
	}

	//[end]

}
